package org.darkforest.controller;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.darkforest.auth.AuthPassport;
import org.darkforest.auth.CurrentMember;
import org.darkforest.common.services.*;
import org.darkforest.common.util.JSONUtils;
import org.darkforest.dao.mapper.GoodsMapper;
import org.darkforest.model.*;

import org.darkforest.vo.AjaxReturnEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 下单结算控制类
 *
 * @auth bingo
 * @create 2017-11-27-18:35
 */
@RestController
@RequestMapping(value = "checkout")
@Api(value = "checkout", description = "直接购买接口 订单金额计算接口 保存订单",produces= MediaType.APPLICATION_JSON_VALUE)
public class CheckoutController {

    @Autowired
    private OrderService orderService;


    @Autowired
    private CheckoutService checkoutService;


    @Autowired
    private SimpleRedisService simpleRedisService;


    @Autowired
    private GoodsMapper goodsMapper;


    /**
     * 直接购买
     * @param member
     * @param goods_id
     * @param quantity
     * @return
     */
    @AuthPassport
    @RequestMapping(value = "outRight")
    @ApiOperation(value = "商品直接购买", httpMethod = "POST",produces= MediaType.APPLICATION_JSON_VALUE)
    public
    RestEntity onceBuy(@CurrentMember Member  member ,Integer goods_id, Short quantity) {
        Goods goodsInfo = goodsMapper.selectByPrimaryKey(goods_id);
        if (goodsInfo.getGoodsState() != 1) {
            return RestEntity.failed("商品已下架");
        }
        if (goodsInfo.getGoodsStorage() < quantity) {
            return RestEntity.failed("库存不足");
        }
        Cart cartAdd = new Cart();
        cartAdd.setGoodsCommonid(goodsInfo.getGoodsCommonid());
        cartAdd.setGoodsNum(quantity);
        cartAdd.setBuyerId(member.getMemberId() + "");
        cartAdd.setGoodsImage(goodsInfo.getGoodsImage());
        cartAdd.setGoodsId(goodsInfo.getGoodsId());
        cartAdd.setGoodsName(goodsInfo.getGoodsName());
        cartAdd.setBlId(0);
        cartAdd.setGoodsPrice(goodsInfo.getGoodsPrice());
        cartAdd.setStoreId(goodsInfo.getStoreId());
        cartAdd.setStoreName(goodsInfo.getStoreName());
        cartAdd.setGoodsClass(goodsInfo.getGcId().toString());
        cartAdd.setChooseGoodsSpec(goodsInfo.getGoodsSpec());
        cartAdd.setSelected("1");
        cartAdd.setGoods(goodsInfo);
        simpleRedisService.setValueInTime(member.getMemberId() + "_buy_goods", JSONObject.fromObject(cartAdd).toString(), 7200L);
        return RestEntity.success();
    }

    /**
     * 订单价格试算
     *
     * @param member    会员
     * @param addressId 收货地址
     * @param qrcodeNum 赠送数量
     * @param buyType   购买方式 1 普通购买  2 二维码赠送
     * @param cartChannel  1 购物车购买  2直接购买
     * @return
     */
    @RequestMapping(value = "orderCheckout")
    @AuthPassport
    @ApiOperation(value = "订单计算", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestEntity orderCheckout(@CurrentMember Member member, Integer addressId,
                                    @RequestParam(defaultValue = "1", required = false) Integer qrcodeNum,
                                    @RequestParam(defaultValue = "1", required = false) Integer buyType,
                                    @RequestParam(defaultValue = "1" ,required = false) Integer cartChannel) {


        Map<String, Object> map = checkoutService.orderCheckout(member, addressId, qrcodeNum, buyType,cartChannel);
        if (map.get("result").equals("true")) {
            return RestEntity.success(map);
        } else {
            return RestEntity.failed(map.get("msg").toString());
        }
    }

    /**
     * @param member
     * @param qrcodeNum
     * @param buyType   购买方式 1 普通购买  2 二维码赠送
     * @param invoiceId 发票ID 如果值为0 则不开发票
     * @param addressId
     * @param cartChannel  1 购物车购买  2直接购买
     * @return
     */
    @RequestMapping(value = "saveOrder")
    @AuthPassport
    @ApiOperation(value = "保存订单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestEntity saveOrder(@CurrentMember Member member,
                                @RequestParam(defaultValue = "1", required = false) Integer qrcodeNum,
                                @RequestParam(defaultValue = "1", required = false) Integer buyType,
                                @RequestParam(defaultValue = "0", required = false) Integer invoiceId,
                                Integer addressId,
                                @RequestParam(defaultValue = "1" ,required = false) Integer cartChannel) {

        AjaxReturnEntity ajaxReturnEntity = new AjaxReturnEntity();
//        if(buyType == 2 && member.getMemberEmailBind() == 0 ){
//            return RestEntity.failed("用户未绑定邮箱，无法使用二维码送礼");
//        }

         ajaxReturnEntity = orderService.saveOrder(buyType, 1, qrcodeNum, addressId, invoiceId, member,cartChannel);

        if (ajaxReturnEntity.isResult()) {
            return RestEntity.success(ajaxReturnEntity.getDatas());
        } else {
            return RestEntity.failed(ajaxReturnEntity.getMessage());
        }
    }
}
